from strategy.StrategyTemplate import StrategyTemplate
from utils.jsl_data_map import default_columns
from strategy import strategy_filter
from strategy.xm_strategy.xm_strategy_common import adjust_column, adjust_combine_from_alive_column, adjust_log_column
from utils.pd_show_utils import df_table
from utils.db import DbHandler
from data_tool.table_data_tools import up_price
import pandas as pd

class BondAdjust(StrategyTemplate):
    """
    下修博弈策略:
        基于下修博弈基础数据计算出详尽信息
    """
    def __init__(self):
        super(BondAdjust, self).__init__()
        self.strategy_name = "下修博弈策略"
        self.strategy_descrption = "下修博弈策略"

    def run(self):
        db = DbHandler()
        db.create_session()
        pd_bond_adjust = pd.read_sql_table('bond_adjust_table', con=db.engine)

        pd_bond_alive = pd.read_sql_table('JSL_bonds_alive', con=db.engine)
        pd_bond_alive = pd_bond_alive[adjust_combine_from_alive_column]

        pd_adjust_log = pd.read_sql_table('adjust_log_table', con=db.engine)
        del pd_adjust_log['bond_nm']
        pd_bond_adjust = pd.merge(pd_bond_adjust, pd_bond_alive, how='left', on='bond_id')
        # print(pd_bond_adjust.describe())
        # pd_bond_adjust = pd.merge(pd_bond_adjust, pd_adjust_log, how='left', on='bond_id')
        pd_bond_adjust = pd_bond_adjust[adjust_column + adjust_combine_from_alive_column]
        pd_bond_adjust['bond_nm'] = pd_bond_adjust['bond_nm'].astype(str) + ' ' + pd_bond_adjust['adjust_tips'].astype(str)
        pd_bond_adjust = up_price(pd_bond_adjust)
        # del pd_bond_adjust['bond_id']
        pd_bond_adjust = pd_bond_adjust.loc[:, ~pd_bond_adjust.columns.duplicated()]

    #
    def api(self, size=10):
        db = DbHandler()
        db.create_session()

        pd_bond_adjust = pd.read_sql_table('bond_adjust_table', con=db.engine)
        pd_bond_adjust['bond_nm'] = pd_bond_adjust['bond_nm'].astype(str) + ' ' + pd_bond_adjust['adj_tips'].astype(str)
        pd_bond_adjust['adjust_count'] = pd_bond_adjust['adjust_count'].astype(str) + ' ' + pd_bond_adjust['unadj_tips'].astype(str)

        pd_bond_alive = pd.read_sql_table('JSL_bonds_alive', con=db.engine)
        pd_bond_alive = pd_bond_alive[adjust_combine_from_alive_column]

        pd_redeem_table = pd.read_sql_table('JSL_bonds_redeem', con=db.engine)
        pd_redeem_table = pd_redeem_table[['bond_id', 'next_put_dt']]

        pd_bond_adjust = pd.merge(pd_bond_adjust, pd_bond_alive, how='left', on='bond_id')
        pd_bond_adjust = pd.merge(pd_bond_adjust, pd_redeem_table, how='left', on='bond_id')
        pd_bond_adjust = pd_bond_adjust[adjust_column + adjust_combine_from_alive_column + ['next_put_dt']]

        pd_bond_adjust = pd_bond_adjust.loc[:, ~pd_bond_adjust.columns.duplicated()]
        pd_bond_adjust = up_price(db, pd_bond_adjust)


        # 清掉原来的索引
        pd_bond_adjust.reset_index(inplace=True, drop=True)
        pd_bond_adjust = pd_bond_adjust.fillna('NONE')
        db.close_session()
        return pd_bond_adjust

if __name__ == '__main__':
    ins = BondAdjust()
    # ins.topN = 20
    ins.run()

